<?php

namespace app\common\model\worker;

use app\common\model\AdminModel;
use app\common\model\BaseModel;
use app\common\model\node\NodeModel;
use DateTime;


class OrderModel extends BaseModel
{
    protected $table = 'order';

    public function _formatList100($list)
    {
        $nodeModel = new NodeModel();
        $authModel = new CustomerAuthModel();
        $customerIds = array_column($list,'customer_id');
        $authList = $authModel->where('customer_id','in',$customerIds)->select()->toArray();
        $customerList = CustomerModel::where('id','in',$customerIds)->column('*','id');
        $nodeList = $nodeModel->getNodeList(1);
        foreach ($list as $k=>$v){
            $list[$k]['customer_info'] = $customerList[$v['customer_id']] ?? [];
            $list[$k]['node_list'] = $nodeList;
            $list[$k]['auth_list'] = $authList[$v['id']];
        }
        return $list;
    }
    public function _formatList($list)
    {

        foreach ($list as $k=>&$v){
            $total_amount = 0;
            $goodsInfos = $v->toArray();

            if(empty($goodsInfos['customerGoodsData'])){
                unset($list[$k]);
                continue;
            }
            //产品预算金额
            foreach($v['customerGoodsData'] as $kk => $vv){
                //商品预算金额，完成预算 和申请预算或申请量房时填写的客户预算没有关系，这个是客户代表完成预算的折后价
                if($vv['budget_id'] && $vv['budget_status'] == 1){
                    $v['customerGoodsData'][$kk]['budget_amount'] = CustomerBudgetDetailModel::where('id',$vv['budget_id'])->value('final_price');
                }else{
                    $v['customerGoodsData'][$kk]['budget_amount'] = 0;
                }
                //签完合同之价格后显示合同
                if($vv['contract_id']){
                    $v['customerGoodsData'][$kk]['budget_amount'] = CustomerContractModel::where('id',$vv['contract_id'])->value('final_price');
                }
                $total_amount += $v['customerGoodsData'][$kk]['budget_amount'];
                //预算金额
                $list[$k]['budget_amount'] = $total_amount ?? 0;//客户金额


                $v['customerGoodsData'][$kk]['behalf_name'] = '';
                //客户代表 预算完成 申请预算后，没有审核，不带名字，是没有申请预算 申请预算后，审核通过，带名字，待预算
                if($vv['budget_id'] && $vv['budget_status'] >= 0){
                    $admin_id = CustomerBudgetModel::where('id',$vv['budget_id'])->value('admin_id');
                    $v['customerGoodsData'][$kk]['behalf_name'] = AdminModel::where('id',$admin_id)->value('nickname');
                }
                $v['customerGoodsData'][$kk]['designer_name'] = '';
                //设计师 量房完成
                if($vv['ruler_id'] && $vv['design_status'] >= 0 ){
                    $admin_id = CustomerRulerModel::where('id',$vv['ruler_id'])->value('admin_id');

                    $v['customerGoodsData'][$kk]['designer_name'] = AdminModel::where('id',$admin_id)->value('nickname');
                }
                $v['customerGoodsData'][$kk]['install_name'] = '';
                if($vv['order_id']){
                    $install_admin_id = OrderModel::where('id',$vv['order_id'])->value('install_admin_id');
                    if($install_admin_id){
                        $v['customerGoodsData'][$kk]['install_name'] = AdminModel::where('id',$install_admin_id)->value('nickname');
                    }

                }



            }

            //推荐人信息
            $list[$k]['recommend_name'] = '';
            $list[$k]['recommend_mobile'] = '';
            $list[$k]['recommend_compay'] = '';
            if($v['recommend_id']){
                $recommendInfo = RecommendModel::where('id',$v['recommend_id'])->field('id,recommend_compay,recommend_name,recommend_mobile')->find();
                $list[$k]['recommend_name'] = $recommendInfo['recommend_name'];
                $list[$k]['recommend_mobile'] = $recommendInfo['recommend_mobile'];
                $list[$k]['recommend_compay'] = $recommendInfo['recommend_compay'];
            }

            //客户所属人
            $admin_id = CustomerAuthModel::where('customer_id',$v['id'])->where('role','create')->value('admin_id');
            $list[$k]['create_man_info'] = AdminModel::where('id',$admin_id)->field('id,mobile,nickname')->find()->toArray();


            $list[$k]['customer_goods_sum'] = $v->customerGoodsData? count( $v->customerGoodsData) : 0;

            //跟进信息
            $customerFollowLogInfo = CustomerFollowLogModel::where('customer_id',$v['id'])->order('id','desc')->find();
            $day_diff = '';
            if($customerFollowLogInfo){
                $date1 = new DateTime("now");

                $date2 = new DateTime($customerFollowLogInfo['create_time']);

                $interval = $date1->diff($date2);
                $day_diff = $interval->d;

            }
            $list[$k]['follow_info'] =
                [
                    'follow_content' => $customerFollowLogInfo['content'] ?? '',
                    'follow_day'     => $day_diff ?? '',
                ];
        }
        return $list;
    }
}
